package ${servicePkg};
import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bjtct.oom.common.vo.Constans;
import com.bjtct.oom.common.vo.Count;
import com.bjtct.oom.common.vo.Result;
import com.bjtct.oom.common.exception.BzException;
 <#if domainPkg?? >
import ${domainPkg}.${name?cap_first}Entity;
import ${domainPkg}.${name?cap_first}Query;
<#else>
import ${defaultPkg}.domain.${name?lower_case}.${name?cap_first}Entity;
import ${defaultPkg}.domain.${name?lower_case}.${name?cap_first}Query;
</#if>
<#if daoPkg??>
import ${daoPkg}.${name?cap_first}Mapper;
<#else>
import ${defaultPkg}.mapper.${name?lower_case}.${name?lower_case}Mapper;
</#if>
<#assign gentime = .now>
/**
*${name}模块对应的业务实现类
*@since ${gentime?string['yyyy-MM-dd HH:mm']}
*@author autogen
*/
@Service("${name?uncap_first}Service")
public class ${name?cap_first}ServiceImpl implements ${name?cap_first}Service{
	public static Logger log = LoggerFactory.getLogger(${name?cap_first}ServiceImpl.class);
	@Autowired
	private ${name?cap_first}Mapper ${name?uncap_first}Mapper;
	
	public ${name?cap_first}Entity get${name?cap_first}ByID(${pkCol.data_type} ${pkCol.column_alias}){
	
		return ${name?uncap_first}Mapper.get${name?cap_first}ByID(${pkCol.column_alias});
	}
<#if showStyle=="TREE"||showStyle=="TREE_TABLE"> 
	public List<${name?cap_first}Entity> get${name?cap_first}Childrens(${pkCol.data_type} ${pkCol.column_alias}) {
		return ${name?uncap_first}Mapper.get${name?cap_first}Childrens(${pkCol.column_alias});
	} 
	public List<${name?cap_first}Entity>  get${name?cap_first}Tree(){
		${name?cap_first}Entity root = get${name?cap_first}ByID(Constans.ROOT_ID);
		List<${name?cap_first}Entity> list = new ArrayList<${name?cap_first}Entity>();
		buildTree(root);
		list.add(root);
		return list;
	}
	private void buildTree(${name?cap_first}Entity obj) {
		List<${name?cap_first}Entity> child = this.get${name?cap_first}Childrens(obj.get${pkCol.column_alias?cap_first}());
		if (child != null && child.size() > 0) {
			obj.setChildren(child);
			for (${name?cap_first}Entity o : child) {
				buildTree(o);
			}
		}
	}
</#if>
 
	public Result<${name?cap_first}Entity> get${name?cap_first}List(${name?cap_first}Query query){
	
		Result<${name?cap_first}Entity> result = new Result<${name?cap_first}Entity>(${name?uncap_first}Mapper.get${name?cap_first}Count(query),
				${name?uncap_first}Mapper.get${name?cap_first}List(query));
		return result;
	}
 
	public List<${name?cap_first}Entity> get${name?cap_first}All(${name?cap_first}Query query){
		return ${name?uncap_first}Mapper.get${name?cap_first}All(query);
	}
 
	public Count get${name?cap_first}Count(${name?cap_first}Query query){
				Count count = new Count(${name?uncap_first}Mapper.get${name?cap_first}Count(query));
		return count;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor={BzException.class})
	public void save${name?cap_first}(${name?cap_first}Entity entity) throws BzException{
		try{
			${name?uncap_first}Mapper.save${name?cap_first}(entity);
		}catch(Exception e){
			log.error(e.getMessage());
			throw new BzException("保存出现错误，请重新操作或联系管理员检查错误！");
		}
	
		
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor={BzException.class})
	public void update${name?cap_first}(${name?cap_first}Entity entity) throws BzException{
		try{
			${name?uncap_first}Mapper.update${name?cap_first}(entity);
		}catch(Exception e){
			log.error(e.getMessage());
			throw new BzException("更新出现错误，请重新操作或联系管理员检查错误！");
		}		
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor={BzException.class})
	public void delete${name?cap_first}(${pkCol.data_type}[] ${pkCol.column_alias}) throws BzException{
		try{
			${name?uncap_first}Mapper.delete${name?cap_first}(${pkCol.column_alias});
		}catch(Exception e){
			log.error(e.getMessage());
			throw new BzException("删除出现错误，请重新操作或联系管理员检查错误！");
		}	
	 }
}